package flc.ast.utils;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

/**
 * Author：daizhongbin on 2021/5/7
 * Email：1027154338@qq.com
 * Desc:
 */
public class TypefaceUtil {

    private static Map<String, Typeface> sMapTypeface = new HashMap<>();

    public static void setViewTypeface(String fontPath, TextView... textViews) {
        if (TextUtils.isEmpty(fontPath) || textViews == null) {
            return;
        }

        Typeface typeface = sMapTypeface.get(fontPath);
        if (typeface != null) {
            if (typeface != null) {
                for (TextView textView : textViews) {
                    textView.setTypeface(typeface);
                }
            }
            return;
        }

        RxUtil.create(new RxUtil.Callback<Typeface>() {
            @Override
            public void doBackground(ObservableEmitter<Typeface> emitter) {
                Typeface typeface = Typeface.createFromAsset(textViews[0].getContext().getAssets(), fontPath);
                emitter.onNext(typeface);
            }

            @Override
            public void accept(Typeface typeface) {
                if (typeface != null) {
                    for (TextView textView : textViews) {
                        textView.setTypeface(typeface);
                    }
                    sMapTypeface.put(fontPath, typeface);
                }
            }
        });
    }

    public static void setViewTypefaceBySdc(TextView... textViews) {
        setViewTypeface("fonts/hxbnt.otf", textViews);
    }
}
